package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.service.CustomerService;
import com.qk.utils.CurrentUserHoler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
@Slf4j
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerService customerService;
    @Override
    public PageResult<Business> querylist(BusinessDto businessDto) {
      Page<Business> page = businessMapper.query(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
      return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public Business selectById(Integer id) {
        return businessMapper.selectId(id);
    }

    @Override
    @Transactional
    public void updateBusiness(@RequestBody Business business) {
        log.info("修改商机{}",business);
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);

    }

    /**
     * 转客户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toCustomer(Integer id) {
        log.info("转客户{}",id);
        Business businessConvert = businessMapper.selectById(id);
        businessConvert.setStatus(5);
        businessConvert.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(businessConvert);
        //创建一个客户对象
        Customer customer = new Customer();
        BeanUtils.copyProperties(businessConvert,customer);
        customer.setId(null);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
    }
    /**
     * 公海池查询
     */
    @Override
    public PageResult<Business> querylistPool(BusinessDto businessDto) {
       Page<Business> page =  businessMapper.queryListPool(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
